package com.chen.palmar.project.set;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.project.init.LoginActivity;
import com.flyco.dialog.widget.NormalDialog;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.net.entity.MessageEvent;
import com.primb.androidlibs.net.exception.ApiException;
import com.primb.androidlibs.utils.DataCleanManager;

import org.greenrobot.eventbus.EventBus;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;

/**
 * @author Chen
 * @date 2018/1/9
 * 功能描述：
 */

public class SettingActivity extends BaseActivity {
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.tv_clear_cache)
    TextView tvClearCache;
    public static final int EVENT_SET_CODE = 10201;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> SettingActivity.this.finish());
        titleBar.setText("设置");

        try {
            String cacheSize = DataCleanManager.getCacheSize(this.getExternalCacheDir());
            tvClearCache.setText(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_settting;
    }

    @OnClick({R.id.ly_clear_cache, R.id.ly_service_contract, R.id.ly_self, R.id.btn_out_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ly_clear_cache:
                NormalDialog dialog = showAlterDialog("清除缓存", "确定要清除缓存吗？");
                dialog.setOnBtnClickL(null, () -> {
                    DataCleanManager.cleanApplicationData(SettingActivity.this);
                    try {
                        String cacheSize = DataCleanManager.getCacheSize(SettingActivity.this.getExternalCacheDir());
                        tvClearCache.setText(cacheSize);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    dialog.dismiss();
                });
                break;
            case R.id.ly_service_contract:
                startActivity(new Intent(SettingActivity.this, ServiceActivity.class));
                break;
            case R.id.btn_out_login:
                NormalDialog dialogOut = showAlterDialog("退出登录", "确定要退出登录吗？");
                dialogOut.setOnBtnClickL(null, () -> {
                    outLogin();
                    EventBus.getDefault().post(new MessageEvent(EVENT_SET_CODE));
                    //重新启动app
                    Intent intent = new Intent(SettingActivity.this, LoginActivity.class);
                    startActivity(intent);
                    dialogOut.dismiss();
                    SettingActivity.this.finish();
                });
                break;
            case R.id.ly_self:
                startActivity(new Intent(SettingActivity.this, SelfInfoActivity.class));
                break;
        }
    }

    private void outLogin() {
        DataCenter.httpConfig.setUserId("-1");
        DataCenter.httpConfig.setLoginToken("soask_5366.com");
        Subscription subscribe = DataCenter.outLoginInfo().subscribe(new HttpSubscriber<HttpResultEntity>(this) {
            @Override
            public void onNext(HttpResultEntity httpResultEntity) {
            }

            @Override
            public void onError(ApiException e) {
            }
        });
        subscription.add(subscribe);

    }
}
